package com.sun.dream.fish.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.sun.dream.fish.entity.TNavMetaEntity;
import com.sun.dream.fish.entity.TRoleMenuEntity;
import com.sun.dream.fish.entity.TUserEntity;
import com.sun.dream.fish.mapper.TNavMetaMapper;
import com.sun.dream.fish.mapper.TRoleMenuMapper;
import com.sun.dream.fish.mapstruct.MenuMapBasic;
import com.sun.dream.fish.mvc.exception.DSFException;
import com.sun.dream.fish.mvc.exception.ValidResult;
import com.sun.dream.fish.service.IMenuService;
import com.sun.dream.fish.util.ResponseUtil;
import com.sun.dream.fish.vo.ResultVO;
import com.sun.dream.fish.vo.back.MenuData;
import com.sun.dream.fish.vo.back.MenuVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class MenuServiceImpl implements IMenuService {

    @Autowired
    TNavMetaMapper navMetaMapper;
    @Autowired
    TRoleMenuMapper roleMenuMapper;

    @Override
    public ResultVO addMenu(MenuVO menuVO) {
        try {
            if (menuVO.getId() != null && menuVO.getId() > 0) {
                menuVO.setId(null);
            }
            navMetaMapper.insert(MenuMapBasic.INSTANCE.voToEntity(menuVO));
        } catch (Exception e) {
            log.error("【IMenuService】异常, ", e);
            return ResponseUtil.fail();
        }
        return ResponseUtil.ok();
    }

    @Override
    public ResultVO<MenuData> listMenu() {
        List<TNavMetaEntity> lists = navMetaMapper.selectList(new LambdaQueryWrapper<>());
        return ResponseUtil.ok(new MenuData(MenuMapBasic.INSTANCE.entityToVO(lists)), "成功");
    }

    @Override
    public ResultVO<MenuData> listRoleMenu() {
        //获取主体
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            return ResponseUtil.fail(1000);
        }
        TUserEntity principal = (TUserEntity) subject.getPrincipal();
        Integer roleId = principal.getRoleId();
        List<TNavMetaEntity> tNavMetaEntities = roleMenuMapper.listMenuByRole(roleId);
        return ResponseUtil.ok(new MenuData(MenuMapBasic.INSTANCE.entityToVO(tNavMetaEntities)), "成功");
    }

    @Override
    public ResultVO<MenuVO> getById(Integer id) {
        TNavMetaEntity tNavMetaEntity = navMetaMapper.selectById(id);
        if (tNavMetaEntity == null) {
            return ResponseUtil.ok("未找到数据");
        }
        return ResponseUtil.ok(MenuMapBasic.INSTANCE.entityToVO(tNavMetaEntity), "成功");
    }

    @Override
    public ResultVO deleteById(Integer id) {
        try {
            navMetaMapper.deleteById(id);
        } catch (Exception e) {
            log.error("【IMenuService】异常, ", e);
            return ResponseUtil.fail("删除异常");
        }
        return ResponseUtil.ok("删除成功");
    }

    @Override
    public ResultVO updateById(MenuVO menuVO) {
        try {
            navMetaMapper.updateById(MenuMapBasic.INSTANCE.voToEntity(menuVO));
        } catch (Exception e) {
            log.error("【IMenuService】异常, ", e);
            return ResponseUtil.fail("修改异常");

        }
        return ResponseUtil.ok("修改成功");
    }
}
